package com.example.jspweb.controller;

import com.example.jspweb.model.VrpRequest;
import com.example.jspweb.model.VrpResponse;
import com.example.jspweb.service.VrpService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vrp")
@RequiredArgsConstructor
public class VrpController {
    private final VrpService vrpService;

    @PostMapping("/solve")
    public ResponseEntity<VrpResponse> solveVrp(@RequestBody VrpRequest request) {
        try {
            // 基本输入验证
            if (request == null || request.getVehicle() == null) {
                VrpResponse response = createEmptyResponse("请提供车辆信息");
                return ResponseEntity.badRequest().body(response);
            }
            
            if (request.getServices() == null || request.getServices().isEmpty()) {
                VrpResponse response = createEmptyResponse("至少需要一个服务地点");
                return ResponseEntity.badRequest().body(response);
            }
            
            // 验证车辆信息
            if (request.getVehicle().getStartLocation() == null) {
                VrpResponse response = createEmptyResponse("请提供车辆起始位置");
                return ResponseEntity.badRequest().body(response);
            }
            
            if (request.getVehicle().getCapacity() <= 0) {
                VrpResponse response = createEmptyResponse("车辆容量必须为正数");
                return ResponseEntity.badRequest().body(response);
            }
            
            // 验证服务点信息
            for (VrpRequest.Service service : request.getServices()) {
                if (service.getLocation() == null) {
                    VrpResponse response = createEmptyResponse("所有服务点都需要提供位置信息");
                    return ResponseEntity.badRequest().body(response);
                }
                if (service.getDemand() < 0) {
                    VrpResponse response = createEmptyResponse("服务需求量不能为负数");
                    return ResponseEntity.badRequest().body(response);
                }
                if (service.getDemand() > request.getVehicle().getCapacity()) {
                    VrpResponse response = createEmptyResponse("服务需求量不能超过车辆容量");
                    return ResponseEntity.badRequest().body(response);
                }
            }

            VrpResponse response = vrpService.solve(request);
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            VrpResponse response = createEmptyResponse("服务器内部错误: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    private VrpResponse createEmptyResponse(String message) {
        VrpResponse response = new VrpResponse();
        response.setCost(0.0);
        response.setStatus("error");
        response.setMessage(message);
        response.setRoutes(java.util.Collections.singletonList(
            new VrpResponse.Route("vehicle_1", 
                new java.util.ArrayList<>(), 
                0.0, 0.0, 0)
        ));
        return response;
    }

    @GetMapping("/health")
    public ResponseEntity<String> healthCheck() {
        return ResponseEntity.ok("VRP服务运行正常");
    }
}
